<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>

    <h1>拦截练习</h1>
    <input type="text">
    <button>提交</button>

    <script>
        const oIpt = document.querySelector("input")
        const oBtn = document.querySelector("button")

        //创建axios分支
        const myAxios = axios.create({
            baseURL: "/",
            timeout: 10000,
            headers: {}
        })

        //给axios分支设置请求拦截
        myAxios.interceptors.request.use(config => {
            //给请求头报文(config.headers)添加一个token
            const token = "agsafaadaw,awefaiuohfoai,dfigjdfig"
            config.headers.token = token

            console.log(config);

            return config
        })


        //添加button的点击事件
        oBtn.onclick = async function () {
            //获取文本框里面的内容
            const inputValue = oIpt.value
            try {
                const re = await myAxios.get("/user-practice", {
                    params: {
                        userId: inputValue
                    }
                })
                console.log(re);
            } catch (e) {
                console.log(e.message);
            }
        }
    </script>
</body>

</html>